export default class Excel {
    
    constructor(store) {
        this.workbook = window.GC.Spread.Sheets.findControl(document.getElementById(`edcWorkbookOfPage${store.pageid}`));
        this.store = store;
    }
    bind = (event, callback) => {
        this.workbook.bind(event, callback);
    }
    getSheets = () => {
        return this.workbook.sheets;
    }
    getActiveSheet = () => {
        return this.workbook.getActiveSheet();
    }
    getDetailTable = (tableName) => {
        const [sheetName, dtName] = tableName.split('.');
        if (sheetName && dtName) {
            var sheet = this.workbook.getSheetFromName(sheetName);
            if (sheet) {
                return sheet.tables.findByName(dtName);
            }
        }
        return null;
    }
    getFieldValue = (fieldName = '') => {
        const [sheetName, fieldOrDetail, detailRow, detailField] = fieldName.split('.');
        if (sheetName) {
            var sheet = this.workbook.getSheetFromName(sheetName);
            if (sheet) {
                const ds = sheet.getDataSource();
                if (ds) {
                    return ds.getValue(fieldName);
                }
            }
        }
        return null;
    }
    setFieldValue = (fieldName, fieldValue) => {
        const [sheetName] = fieldName.split('.');
        if (sheetName) {
            var sheet = this.workbook.getSheetFromName(sheetName);
            if (sheet) {
                const ds = sheet.getDataSource();
                if (ds) {
                    this.workbook.suspendPaint();
                    ds.setValue(fieldName, fieldValue);
                    this.store.calcAll(true, sheetName);
                    this.workbook.resumePaint();
                }
            }
        }
    }
}